#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
MCP 计算器服务
"""

from mcp.server.fastmcp import FastMCP

# 创建 MCP 服务实例
mcp = FastMCP("Calculator Service")

@mcp.tool()
def add(numbers: list) -> str:
    """加法运算
    Args:
        numbers: 要相加的数字列表
    Returns:
        相加结果
    """
    try:
        return f"总和: {sum(numbers)}"
    except Exception as e:
        return f"计算失败: {str(e)}"

@mcp.tool()
def subtract(a: float, b: float) -> str:
    """减法运算
    Args:
        a: 被减数
        b: 减数
    Returns:
        相减结果
    """
    try:
        return f"结果: {a - b}"
    except Exception as e:
        return f"计算失败: {str(e)}"

@mcp.tool()
def multiply(numbers: list) -> str:
    """乘法运算
    Args:
        numbers: 要相乘的数字列表
    Returns:
        相乘结果
    """
    try:
        result = 1
        for num in numbers:
            result *= num
        return f"乘积: {result}"
    except Exception as e:
        return f"计算失败: {str(e)}"

@mcp.tool()
def divide(a: float, b: float) -> str:
    """除法运算
    Args:
        a: 被除数
        b: 除数
    Returns:
        相除结果
    """
    try:
        if b == 0:
            return "错误: 除数不能为零"
        return f"结果: {a / b}"
    except Exception as e:
        return f"计算失败: {str(e)}"

if __name__ == "__main__":
    mcp.run(transport="stdio")